from django.shortcuts import render,redirect
#导包
from django.http import HttpResponse,HttpResponseRedirect,JsonResponse
#导入类视图
from django.urls import reverse
from django.views import View

from myapp.models import User
import json
from django.core.serializers import serialize
from rest_framework.response import Response
from rest_framework.views import APIView
#导入加密库
import hashlib
#导入图片库
#绘画库
from PIL import ImageDraw
#字体库
from PIL import ImageFont
#图片库
from PIL import Image
#随机库
import random
#文件流
import io

import requests

#导入上传文件夹配置
from mydjango import settings
from mydjango.settings import UPLOAD_ROOT
import os

#导入原生sql模块
from django.db import connection

import jwt

#导入redis数据库
import redis

#导入时间模块
import time

#导入公共目录变量
from mydjango.settings import BASE_DIR

#导包
from django.db.models import Q,F

#导入dwebsocket的库
from dwebsocket.decorators import accept_websocket
import uuid


# 方法视图
def myindex(request):

    return HttpResponse('这里是首页')

# 类视图 可读性好，易于扩展
class MyView(View):
    greeting = '你好,这里是首页'

    def get(self, request):
        # title = 'hello django'
        # num = 120
        # li = [1, 2, 3, 4, 5]
        # dic = {'name': '星', 'age': 23}

        # return HttpResponse(self.greeting)

        # 渲染模板
        return render(request, 'text.html', locals())
    
    def post(self, request):
        return HttpResponse(self.greeting)

# 首页
def index(request):
    if request.method == 'GET':
        # 取出表中所有数据
        user_list = User.objects.all()
        return render(request, 'index.html', locals())

# 增加
def create(request):
    if request.method == 'POST':
        user = request.POST.get('user')
        pwd = request.POST.get('pwd')
        img = request.FILES.get('img')
        type = request.POST.get('type')
        file = os.path.join(settings.UPLOAD_ROOT, img.name)
        f = open(file, 'wb')
        for i in img.chunks():
            f.write(i)
        f.close()
        User.objects.create(username=user, password=pwd, img=img, type=type)
        return redirect(reverse('index'))
    return render(request, 'create.html', locals())

# 删除
def delete(request):
    if request.method == 'GET':
        id = request.GET.get('id')
        User.objects.filter(id=id).delete()
        return redirect(reverse('index'))
    return redirect(reverse('index'))

# 修改
def change(request):
    if request.method == 'GET':
        id = request.GET.get('id')
        user = User.objects.filter(id=id).first()
        print(id)
        return render(request, 'change.html', locals())
    if request.method == 'POST':
        id = request.POST.get('id')
        user = request.POST['user']
        pwd = request.POST['pwd']
        img = request.FILES.get('img')
        file = os.path.join(settings.UPLOAD_ROOT, img.name)
        f = open(file, 'wb')
        for i in img.chunks():
            f.write(i)
        f.close()
        User.objects.filter(id=id).update(username=user, password=pwd, img='static/upload/'+img.name)
        return redirect(reverse('index'))
    return render(request, 'change.html')